# -*- coding: utf-8 -*-

from time import *

import  wx
import  os
import  wx.wizard as wiz

#import  images
import  encodings.mbcs
import  codecs
import GenericTable
import wx.grid
import validator

import wizdata

#from Main import opj

"""
  png = wx.Image(opj('bitmaps/image.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap()
    jpg = wx.Image(opj('bitmaps/image.jpg'), wx.BITMAP_TYPE_JPEG).ConvertToBitmap()

    panel = wx.Panel(nb, -1)

    pos = 10
    wx.StaticBitmap(panel, -1, bmp, (10, pos), (bmp.GetWidth(), bmp.GetHeight()))

    pos = pos + bmp.GetHeight() + 10
    wx.StaticBitmap(panel, -1, gif, (10, pos), (gif.GetWidth(), gif.GetHeight()))

    pos = pos + gif.GetHeight() + 10
    wx.StaticBitmap(panel, -1, png, (10, pos), (png.GetWidth(), png.GetHeight()))

    pos = pos + png.GetHeight() + 10
    wx.StaticBitmap(panel, -1, jpg, (10, pos), (jpg.GetWidth(), jpg.GetHeight()))

    return panel

#----------------------------------------------------------------------


 
"""

def opj(path):
    """Convert paths to the platform-specific separator"""
    str = apply(os.path.join, tuple(path.split('/')))
    # HACK: on Linux, a leading / gets lost...
    if path.startswith('/'):
        str = '/' + str
    return str
    
class SelectorPage(wiz.PyWizardPage):
    def __init__(self, parent, title):
        #wiz.WizardPageSimple.__init__(self, parent)        
        wiz.PyWizardPage.__init__(self, parent)
        self.next = self.prev = None        
        self.parent = parent
        self.name = ""
        self.built = False
        self.sizer = wx.GridBagSizer(hgap=5, vgap=5)
        #wizPg.SetSizer(sizer)
        title = wx.StaticText(self, -1, title)
        title.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
        self.sizer.Add(title, pos = (0,1),span = (1,4))    

        tc = wx.StaticText(self, -1, u"文件名:",size = (100,-1), style = wx.ALIGN_RIGHT)
        self.sizer.Add(tc, pos=(2,1))
        
        self.filename = wx.TextCtrl(self, validator=validator.CharValidator(), size=(200,-1))
        #self.filename = wx.TextCtrl(self, size=(200,-1))
        self.filename.SetEditable(False)
        
        bt = wx.Button(self, -1, u"选择文件")
        self.Bind(wx.EVT_BUTTON, self.OnFile, bt)
        self.Bind(wx.EVT_TEXT, self.OnText,self.filename)
        
        
        self.sizer.Add(self.filename,pos=(2,2))
        self.sizer.Add(bt,pos=(2,3))
        """
        png = wx.Image(opj('flow.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        pos = 10
        img = wx.StaticBitmap(self, -1, png, (10, pos), (png.GetWidth(), png.GetHeight()))  
        self.sizer.Add(img,pos=(4,1),span=(1,4))
        """
        self.SetSizer(self.sizer)
        self.Fit()
        
    def SetNext(self, next):
        self.next = next
      
    def SetPrev(self, prev):
        self.prev = prev
    
    def GetNext(self):
        next = self.next
        return next
        
    def GetPrev(self):
        return self.prev        
        
    def Build(self):
        
        if self.build == False:
            bt = wx.Button(self, -1, u"选择文件")
            self.sizer.Add(bt,pos=(3,3))
            self.SetSizer(self.sizer)
            self.Fit()
            self.build = True
            
    
    def OnText(self, event):
        #print self.filename.GetValue()
        #print self.filename.GetDefaultStyle()
        nextButton = self.parent.FindWindowById(wx.ID_FORWARD)
        nextButton.Enable(True)
        event.Skip()
        
    def OnFile(self, event):
        
        data = wizdata.WizData()
        
        file = wx.FileSelector(u"选择文件", default_path="", default_filename="",default_extension="", wildcard="*.xls", flags=0, parent=self,x=-1, y=-1)
        
        #print name
        if file != "":
            self.name = file
            #print file
        if file =="" and self.name != "":
            file = self.name
        
        name = os.path.split(file)
        statinfo = os.stat(file)
        (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime,  st_mtime, st_ctime) =  statinfo
        #print statinfo
        data.excelfile.filename = file
        data.excelfile.fsize = st_size
        data.excelfile.mtime =  strftime("%Y-%m-%d %H:%M:%S", gmtime(st_mtime))
        data.excelfile.ctime =  strftime("%Y-%m-%d %H:%M:%S", gmtime(st_ctime))
        
        self.filename.SetValue(name[1])
        nextButton = self.parent.FindWindowById(wx.ID_FORWARD)
        nextButton.Enable(True)
        
        self.SetFilePath()
        
        event.Skip()
        
    def GetFilePath(self):
        return self.name
        
    def SetFilePath(self):
        data = wizdata.WizData()
        data.filename = self.name

class DataFile:
    def __init__(self, filename):
        self.filename = filename
        statinfo = os.stat(self.filename)
        (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, self.st_size, st_atime,  self.st_mtime, self.st_ctime) =  statinfo

    def getSize(self):
        return self.st_size
    
    def getMTime(self):
        return self.st_mtime
        
    def getCTime(self):
        return self.st_ctime
        
    def getShortName(self):
        name = os.path.split(file)
        return name[1]

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "", size=(800, 660)) 
        wizard = wiz.Wizard(self, -1, u"数据导入向导")
        page1 = SelectorPage(wizard, u"选择文件")
        wizard.FitToPage(page1)
        wizard.RunWizard(page1)

def test():
    app = wx.PySimpleApp()
    TestFrame().Show()
    app.MainLoop() 


if __name__ == "__main__":
    test()
